package com.demo.petshop.service.impl;

import com.demo.petshop.entity.Addresses;
import com.demo.petshop.mapper.AddressesMapper;
import com.demo.petshop.service.AddressesService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.demo.petshop.entity.table.AddressesTableDef.ADDRESSES;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@Service
public class AddressesServiceImpl extends ServiceImpl<AddressesMapper, Addresses> implements AddressesService {

    @Autowired
    private AddressesMapper addressesMapper;
    @Override
    public Object setDefaultAddress(Integer addressId, Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select(ADDRESSES.ALL_COLUMNS).from(ADDRESSES)
                .where(ADDRESSES.ADDRESS_ID.eq(addressId))
                .and(ADDRESSES.USER_ID.eq(userId));
        Addresses addresses = addressesMapper.selectOneByQuery(queryWrapper);
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.select(ADDRESSES.ALL_COLUMNS).from(ADDRESSES)
                .where(ADDRESSES.IS_DEFAULT.eq(1));
        Addresses addresses1 = addressesMapper.selectOneByQuery(queryWrapper1);
        if(addresses1!=null){
            addresses1.setIsDefault(0);
            addressesMapper.update(addresses1);
        }
        addresses.setIsDefault(1);
        return addressesMapper.update(addresses);
    }

    @Override
    public Object getUserAddresses(Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select(ADDRESSES.ALL_COLUMNS).from(ADDRESSES)
                .where(ADDRESSES.USER_ID.eq(userId));
        return addressesMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Object saveAddress(Addresses addresses) {
        if(addresses.getIsDefault()==1){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select(ADDRESSES.ALL_COLUMNS).from(ADDRESSES)
                .where(ADDRESSES.USER_ID.eq(addresses.getUserId()));
        List<Addresses> addresses1 = addressesMapper.selectListByQuery(queryWrapper);
        for(Addresses address : addresses1){
            address.setIsDefault(0);
            addressesMapper.update(address);
        }
    }
        return addressesMapper.insert(addresses);
    }

    @Override
    public Object updateAddress(Addresses addresses) {
        if(addresses.getIsDefault()==1){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.select(ADDRESSES.ALL_COLUMNS).from(ADDRESSES)
                    .where(ADDRESSES.USER_ID.eq(addresses.getUserId()));
            List<Addresses> addresses1 = addressesMapper.selectListByQuery(queryWrapper);
            for(Addresses address : addresses1){
                address.setIsDefault(0);
                addressesMapper.update(address);
            }
        }
        return addressesMapper.update(addresses);
    }
}
